<script setup lang="ts">
import { ref, onMounted, onUnmounted ,computed,  watch} from 'vue'

import { Handle, Position, useVueFlow } from '@vue-flow/core'
defineOptions({
  name: 'HttpAudioInNode'
})

const props = defineProps<{
  id: string;
  data: {
    name: string;
    route?: string;
    enWS?: boolean;
    enInterrupt?: boolean;    
    pulseSeconds?: number;
    sampleRate?: number
    channels?: number
    bitDepth?: number
  };
  style?: Record<string, any>; // 添加 style 属性
}>()

const { updateNodeData } = useVueFlow()
const name = computed({
  get: () => props.data.name,
  set: (name) => updateNodeData(props.id, {...props.data, name: name }),
})
import useConnectionValid from './ts/connectionValid';
const { isValidConnection } = useConnectionValid();


// 节点属性
const route = ref(props.data.route || '');
const enWS = ref(props.data.enWS);
const enInterrupt = ref(props.data.enInterrupt);
const sampleRate = ref(props.data.sampleRate);
const channels = ref(props.data.channels);
const bitDepth = ref(props.data.bitDepth);
const pulseSeconds = ref(props.data.pulseSeconds);
watch(() => [route.value, enWS.value, enInterrupt.value, sampleRate.value, channels.value, bitDepth.value, pulseSeconds.value], ()=>{
  updateNodeData(props.id, {
    ...props.data,
    route: route.value,
    enWS: enWS.value,
    enInterrupt: enInterrupt.value,
    sampleRate: sampleRate.value,
    channels: channels.value,
    bitDepth: bitDepth.value,
    pulseSeconds: pulseSeconds.value
  })
})
</script>

<template>
  
    <div class="NodeName"> <input :id="`${id}-input`" v-model="name"  class="nodrag" /> </div>
    <div class="NodeContent">
      <Handle id="flow-next"  type="source" :class="['AgentFlowOutputHandle','top-0']"  :position="Position.Right"  title="FLow Next" >
        <span class="HandleRightSpan" :connectable="()=>{return isValidConnection(props.id,'flow-next', 'source')}">Flow Next</span> </Handle>
      <Handle id="cached-audio"  type="source" :class="['AgentAudio','top-2']" :position="Position.Right"  title="Cached Audio"  :connectable="()=>{return isValidConnection(props.id,'cached-audio', 'source')}">
        <span class="HandleRightSpan">Cached Audio</span></Handle>
      <Handle id="last-cached-audio"  type="source" :class="['AgentAudio','top-3']" :position="Position.Right"  title="Last Cached Audio Clip"  :connectable="()=>{return isValidConnection(props.id,'last-cached-audio', 'source')}">
        <span class="HandleRightSpan">Last Cached Audio Clip</span></Handle>
      <Handle id="interrupt-flow-next"  type="source" :class="['AgentFlowOutputHandle','top-4']" :position="Position.Right"  title="Interrupt Flow" :connectable="()=>{return isValidConnection(props.id,'interrupt-flow-next', 'source')}"><span class="HandleRightSpan">Interrupt Flow</span></Handle>


      <div class="NodeData">
        <!-- 第一行：textinput -->
        <div class="input-row">
          <label for="textInput">Route:</label>
          <input
            id="textInput"
            type="text"
            v-model="route"
            placeholder="/your-route"
            :class="['text-input','nodrag']"
          />
        </div>

        <!-- 第二行：checkbox 1 -->
        <div class="checkbox-row">
          <label for="enWS">Enable WebSocket</label>
          <input
            id="enWS"
            type="checkbox"
            v-model="enWS"
            :class="['checkbox','nodrag']"
          />
        </div>

        <!-- 第三行：checkbox 2 -->
        <div class="checkbox-row">
          <label for="enInterrupt">Enable Interrupt</label>
          <input
            id="enInterrupt"
            type="checkbox" 
            v-model="enInterrupt"
            :class="['checkbox','nodrag']"
          />
        </div>
        <div class="input-row">
          <label for="pulseSeconds">Pulse Per Seconds:</label>
          <input
            id="pulseSeconds"
            type="number"
            v-model="pulseSeconds"
            placeholder="1"
            :class="['text-input','nodrag']"
          />
        </div>
        <div class="input-row">
          <label for="sampleRate">Sample Rate (kHz):</label>
          <input
            id="sampleRate"
            type="number"
            v-model="sampleRate"
            placeholder="16"
            :class="['text-input','nodrag']"
          />
        </div>
        <div class="input-row">
          <label for="channels">Channels:</label>
          <input
            id="channels"
            type="number"
            v-model="channels"
            placeholder="1"
            :class="['text-input','nodrag']"
          />
        </div>
        <div class="input-row">
          <label for="bitDepth">Bit Depth:</label>
          <input
            id="bitDepth"
            type="number"
            v-model="bitDepth"
            placeholder="16"
            :class="['text-input','nodrag']"
          />
        </div>
      </div>
    </div>
</template>

<style scoped>
.audio-node {
  display: flex;
  justify-content: center;
  padding: 10px;
}

.mic-button {
  padding: 8px 16px;
  border: none;
  border-radius: 4px;
  background-color: #f0f0f0;
  cursor: pointer;
  transition: all 0.3s ease;
}

.mic-button:hover {
  background-color: #e0e0e0;
}

.mic-button.recording {
  background-color: #ff4444;
  color: white;
}

.mic-button.recording:hover {
  background-color: #cc0000;
}
</style> 